#include "MyClass.hh"


MyClass::MyClass(ClassMate* firstClassMate, ClassMate* secondClassMate){
  m_headPtr = firstClassMate;
  m_tailPtr = secondClassMate;
  m_headPtr->m_previousClassMate = nullptr;
  m_headPtr->m_nextClassMate = secondClassMate;
  m_tailPtr->m_previousClassMate = firstClassMate;
  m_tailPtr->m_nextClassMate = nullptr;
}



MyClass::~MyClass(){
  ClassMate *ptr2 = nullptr;
  
  while(m_headPtr!=nullptr){
    ptr2=m_headPtr->m_nextClassMate;
    delete m_headPtr;
    m_headPtr=ptr2;
  }
  m_tailPtr = nullptr;
  
}

void MyClass::printClassMates(){
  ClassMate *aMate = m_headPtr;
  while(aMate!=nullptr){
    aMate->printInfo();
    if(aMate==m_tailPtr){
      break;
    }
    else{
      aMate = aMate->m_nextClassMate;
    }
  }
}

bool MyClass::append(ClassMate* aMate){
  // -->......-->  left(tail) -----> right(nullptr) 
  //                            ^
  //                         middle(aMate)
  
  ClassMate *ptr_left = m_tailPtr;
  ClassMate *ptr_right = nullptr; //  m_tailPtr->m_nextClassMate;
  
  ClassMate *ptr_middle = aMate;
  
  ptr_left->m_nextClassMate = ptr_middle;
  ptr_middle->m_previousClassMate = ptr_left;
  
  ptr_middle->m_nextClassMate = ptr_right; // nullptr

  m_tailPtr = ptr_middle;

  //
  return true;
}

bool MyClass::insertAfter(ClassMate* aMate, ClassMate* refMate){
  //
  //
  
  

  //
  return true;
}

bool MyClass::remove(ClassMate* aMate){
  //
  //


  //
  return true;
}

ClassMate* MyClass::findByMateName(std::string mateName){
  ClassMate *aMateInClass = m_headPtr;
  while(aMateInClass!=nullptr){
    if(aMateInClass->m_name == mateName){
      return aMateInClass;
    }else {
      aMateInClass = aMateInClass->m_nextClassMate;
    }
  }
  return nullptr;
}


bool MyClass::find(ClassMate* aMate){
  ClassMate *aMateInClass = m_headPtr;
  while(aMateInClass!=nullptr){
    if(aMateInClass == aMate){
      return true;
    }else {
      aMateInClass = aMateInClass->m_nextClassMate;
    }
  }
  return false;
}


ClassMate* MyClass::findLeastNameMate(){
  ClassMate *leastNameMate = m_headPtr;
  ClassMate *aMateInClass = m_headPtr->m_nextClassMate;
  while(aMateInClass!=nullptr){
    if(aMateInClass->m_name < leastNameMate->m_name){
      leastNameMate = aMateInClass;
    }
    aMateInClass = aMateInClass->m_nextClassMate;
  }

  return leastNameMate;
}
